<template>
  <div class="app-container">
    <div class="custom_card_0">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="车辆级别" prop="rankName">
          <el-input
            v-model="queryParams.rankName"
            placeholder="请输入车联级别"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
<!--        <el-form-item label="车型" prop="carInfo">-->
<!--          <el-input-->
<!--            v-model="queryParams.carInfo"-->
<!--            placeholder="请输入车辆品牌或车系或车型信息"-->
<!--            clearable-->
<!--            size="small"-->
<!--            @keyup.enter.native="handleQuery"-->
<!--          />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="状态" prop="isAvailable">-->
<!--          <el-select v-model="queryParams.isAvailable" placeholder="请选择是否启用" clearable size="small">-->
<!--            <el-option-->
<!--              v-for="dict in dict.type.sys_available"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--            />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="custom_card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            class="first-plain-btn"
            size="medium"
            icon="el-icon-plus"
            @click="handleAdd"
            v-hasPermi="['car:rank:add']"
          >新增
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="rankList" stripe
                :header-cell-style="{ background: '#eef1f6', color: '#606266' }" border>
        <template slot="empty">
          <el-empty :image-size="50" description="暂无车辆级别信息"></el-empty>
        </template>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  width="200"
                         fixed="left">
          <template slot-scope="scope">

            <el-button
              v-if="scope.row.isAvailable == 0"
              size="mini"
              type="text"
              @click="isAvailableRank(scope.row,1)"
              v-hasPermi="['car:rank:edit']"
            >启用
            </el-button>
            <el-button
              v-if="scope.row.isAvailable == 1"
              size="mini"
              type="text"
              @click="isAvailableRank(scope.row,0)"
              v-hasPermi="['car:rank:edit']"
            >禁用
            </el-button>
<!--            <el-button-->
<!--              size="mini"-->
<!--              type="text"-->
<!--              @click="relatedModels(scope.row)"-->
<!--              v-hasPermi="['car:rank:relatedModels']"-->
<!--            >关联车型-->
<!--            </el-button>-->
            <el-button
              v-if="scope.row.isAvailable == 0"
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['car:rank:edit']"
            >编辑
            </el-button>
            <el-button
              v-if="scope.row.isAvailable == 0 "
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
              v-hasPermi="['car:rank:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="车辆级别" align="center" prop="rankName" min-width="150"/>
<!--        <el-table-column label="关联车型" align="center" prop="rankName" min-width="150"/>-->
<!--        <el-table-column label="排序" align="center" prop="rankName" min-width="150"/>-->
        <el-table-column label="创建时间" align="center" min-width="150">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="所属租户" align="center" prop="tenantName" min-width="150"/>
        <el-table-column label="是否启用" align="center" prop="isAvailable" min-width="150">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_available" :value="scope.row.isAvailable"/>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改车辆级别对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="车辆级别名称" prop="rankName">
          <el-input style="width: 80%" v-model="form.rankName" placeholder="请输入车辆级别名称" maxlength="20"/>
        </el-form-item>
<!--        <el-form-item label="关联车型" prop="rankName">-->
<!--          <el-select style="width: 80%" v-model="form.models" placeholder="请选择关联车型" multiple>-->
<!--            <el-option-->
<!--              v-for="item in carLists"-->
<!--              :key="item.value"-->
<!--              :label="item.label"-->
<!--              :value="item.value">-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="排序" prop="sort">-->
<!--          <el-input-number v-model.number="form.sort" :min="1"  :precision="0" label="序号"></el-input-number>-->
<!--        </el-form-item>-->
        <el-form-item label="是否启用" prop="isAvailable">
          <el-switch
            v-model="form.isAvailable"
            active-value="1"
            inactive-value="0"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="关联车型" :visible.sync="dialogFormVisible" width="500px">
      <el-form :model="carform">
        <el-form-item label="关联车型" label-width="120">
          <el-select style="width: 80%" v-model="carForm.models" placeholder="请选择关联车型" multiple>
            <el-option
              v-for="item in carLists"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listRank, getRank, delRank, addRank, updateRank, exportRank, isAvailableRank} from "@/api/car/rank";

export default {
  name: "Rank",
  dicts: ['sys_available'],
  data() {
    return {
      dialogFormVisible:false,
      carLists: [{
        value: 1,
        label: '车型1'
      }, {
        value: 2,
        label: '车型2'
      }],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆级别表格数据
      rankList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rankName: null,
        isAvailable: null,
        tenantId: null,
        carInfo: null,
      },
      // 新增表单参数
      form: {},
      carForm:{
        models:[1,2]
      },
      // 表单校验
      rules: {

        rankName: [{required: true, message: "车辆级别名称不能为空", trigger: 'blur'}],
        sort: [{required: true, message: "请输入排序序号", trigger: 'blur'}],
        isAvailable: [{required: true, message: "是否启用必选", trigger: 'blur'}],

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询车辆级别列表 */
    getList() {
      this.loading = true;
      listRank(this.queryParams).then(response => {
        this.rankList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rankId: null,
        rankName: null,
        delFlag: null,
        isAvailable: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        tenantId: null,
        sort:null,
        models:[1,2]
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rankId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆级别";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rankId = row.rankId || this.ids
      getRank(rankId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆级别";
      });
    },
    /*关联车型*/
    relatedModels(item){
     this.dialogFormVisible = true;
    },
    /** 启停用按钮操作 */
    isAvailableRank(row, isAvailable) {
      const data = {
        rankId: row.rankId,
        isAvailable: isAvailable
      };
      const tip = 1 == isAvailable ? '启用' : '停用';
      this.$modal.confirm('是否确认' + tip + '车辆级别"' + row.rankName + '"的数据项？').then(function () {
        return isAvailableRank(data);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("启用成功");
      }).catch(() => {
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rankId != null) {
            updateRank(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRank(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rankIds = row.rankId || this.ids;
      this.$modal.confirm('是否确认删除车辆级别？').then(function () {
        return delRank(rankIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有车辆级别数据项？').then(() => {
        this.exportLoading = true;
        return exportRank(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
